#include<Arduino.h> 
#include"TM1637.h"                                          //头文件
int led[8]={2,0,4,16,17,5,18,19};                           //led灯引脚
int i,beep=1;                                               //蜂鸣器
const char key[4]={27,26,25,33};                            //定义按键
#define down 0                                              //定义down=0
#define up 1                                                //定义up=1
#define CLK 21                                              //定义CLK引脚=21
#define DIO 3                                               //定义DIO引脚=3
TM1637 tm1637(CLK,DIO);                                     //定义TM1637对象

void beepd(int x,int y)                                     //定义蜂鸣器
{
   tone(beep,y);                                            //开蜂鸣器，定义频率
   delay(x);                                                //延长x毫秒
   noTone(beep);                                            //关蜂鸣器
   delay(1000);                                             //延长1秒
}

void setup()                                                //setup函数
{
   for(i=0;i<8;i++)                                         //循环led灯output
   {
      pinMode(led[i],OUTPUT);
   }
   for(i=0;i<4;i++)                                         //循环按键input
   {
      pinMode(key[i],INPUT_PULLUP);
   }
      tm1637.init();                                        //初始化TM1637
      tm1637.point(0);                                      //设置小数点 关闭
      tm1637.set(BRIGHT_TYPICAL);                                                       
}

void liushiu()                                              //流水灯1：流水
{                                                           //流水灯1号
   for(i=0;i<8;i++)                                         //循环正单亮
   {
      digitalWrite(led[i],HIGH);                            //亮
      delay(300);                                           //延长0.3秒
      digitalWrite(led[i],LOW);                             //关
   }
   for(i=7;i>=0;i--)                                        //循环反单亮亮
   {
      digitalWrite(led[i],HIGH);                            //亮
      delay(300);                                           //延长0.3秒
      digitalWrite(led[i],LOW);                             //关
   }
   digitalWrite(led[0],HIGH);                               //流水灯2号
    delay(300);
    for(i=0;i<7;i++)
   {
      digitalWrite(led[i],HIGH);                            //i灯亮
      digitalWrite(led[i+1],HIGH);                          //i+1灯亮
      delay(300);                                           //延长0.3秒
      digitalWrite(led[i],LOW);                             //i灯灭
      digitalWrite(led[i+1],LOW);                           //i+1灯灭
   }
   digitalWrite(led[7],HIGH);                               //7号灯亮
   delay(300);                                              //延长0.3秒
   digitalWrite(led[7],LOW);                                //7号灯灭
}

void baoshan()                                              //流水灯2：爆闪
{
   for(int c=0;c<4;c++)                                     //全闪循环4次
   {
      tone(beep,888);                                       //蜂鸣器发声
      for(i=0;i<8;i++)                                      //全亮
      {
         digitalWrite(led[i],HIGH);
      }
      delay(300);                                           //延长0.3秒
      noTone(beep);                                         //蜂鸣器停止发声
      for(i=0;i<8;i++)                                      //全灭
      {
         digitalWrite(led[i],LOW);
      }
      delay(300);                                           //延长0.3秒
      c++;                                                  //计数
   }
   int c=0;                                                 //计数清零
   while(c<3)                                               //半闪循环
   {
      tone(beep,1000);                                      //蜂鸣器发声
      for(i=0;i<4;i++)                                      //左亮
      {
         digitalWrite(led[i],HIGH);
      }
      delay(300);                                           //延长0.3秒
      noTone(beep);                                         //蜂鸣器停止发声
      for(i=0;i<4;i++)                                      //左灭
      {
         digitalWrite(led[i],LOW);                              
      }
      delay(300);                                            //延长0.3秒
      tone(beep,1000);                                       //蜂鸣器发声
      for(i=4;i<8;i++)                                       //右亮
      {
         digitalWrite(led[i],HIGH);
      }
      delay(300);                                            //延长0.3秒
      noTone(beep);                                          //蜂鸣器停止发声
      for(i=4;i<8;i++)                                       //右灭
      {
         digitalWrite(led[i],LOW);
      }
      delay(300);                                            //延长0.3秒
      c++;                                                   //次数加1
   }
}

void geshan()                                               //流水灯3：隔闪
{
    int k=1,c=0;                                            //定义i，c
   for(k=1;c<=4;c++)                                        //次数
   {
      switch(k)                                             //选择
      {case 0:                                              //当K=0时
      {
         k=k+1;                                             //k加1
         for(i=0;i<=6;i=i+2)                                //单数亮
         {
            digitalWrite(led[i],HIGH);
         }
         delay(500);
         for(i=0;i<=6;i=i+2)                                //单数灭
         {
            digitalWrite(led[i],LOW);
         }
         delay(500);};break;
      case 1:                                               //当k=1时
         {k=k-1;
         for(i=1;i<=7;i=i+2)                                //双数亮
         {
            digitalWrite(led[i],HIGH);
         }
         delay(500);
         for(i=1;i<=7;i=i+2)                                //双数灭
         {
            digitalWrite(led[i],LOW);
         }
         delay(500);};break;
      }
   }
}

void niganma()                                              //流水灯4：音量灯
{
   for(i=0;i<8;i++)                                         //音量灯显示
   {
      digitalWrite(led[i],HIGH);
      delay(300);
   }
   for(i=7;i>=0;i--)
   {
      digitalWrite(led[i],LOW);
      delay(300);
   }
   tone(beep,500);                                          //你干吗，，，一下午没做成
   digitalWrite(led[0],HIGH);
   delay(100);
   digitalWrite(led[1],HIGH);
   delay(900);
   tone(beep,1200);
   digitalWrite(led[2],HIGH);
   delay(20);
   digitalWrite(led[3],HIGH);
   delay(50);
   digitalWrite(led[4],HIGH);
   delay(50);
   tone(beep,1800);
   digitalWrite(led[5],HIGH);
   delay(10);
   digitalWrite(led[6],HIGH);
   delay(300);
   tone(beep,1000);
   digitalWrite(led[6],LOW);
   delay(50);
   digitalWrite(led[5],LOW);
   delay(50);
   digitalWrite(led[4],LOW);
   delay(300);
   tone(beep,1300);
   digitalWrite(led[4],HIGH);
   delay(800);
   noTone(beep);
   digitalWrite(led[4],LOW);
   delay(100);
   digitalWrite(led[3],LOW);
   delay(100);
   digitalWrite(led[2],LOW);
   delay(100);
   digitalWrite(led[1],LOW);
   delay(100);
   digitalWrite(led[0],LOW);
   delay(100);
}

void shumaguanshezhi(int x,int y)                           //数码管设置
{
      tm1637.display(x,y);                                  //x数码管显示y
      delay(500);                                           //延时
      tm1637.clearDisplay();                                //清除数码管显示
      delay(300);                                           //延时
      tm1637.display(x,y);                                  //x数码管显示y
      delay(500);                                           //延时
      tm1637.clearDisplay();                                //清除数码管显示
}

void loop()                                                 //loop
{
   if(digitalRead(key[0])==down)                            //按键0按下
   {
      delay(10);                                            //延时
      while(digitalRead(key[0])==down);                     //等待按键0松开
      shumaguanshezhi(0,1);                                 //数码管设置
      beepd(500,333);                                       //蜂鸣器响
      liushiu();                                            //1
      beepd(300,555);                                       //延时
   }
   else if(digitalRead(key[1])==down)                       //按键1按下
   {
      delay(10);                                            //延时
      while(digitalRead(key[1])==down);                     //等待按键1松开
      shumaguanshezhi(1,2);                                 //数码管设置
      beepd(500,333);                                       //蜂鸣器响
      baoshan();                                            //2
      beepd(300,555);                                       //延时
   }
   else if(digitalRead(key[2])==down)                       //按键2按下
   {
      delay(10);                                            //延时
      while(digitalRead(key[2])==down);                     //等待按键2松开
      shumaguanshezhi(2,3);                                 //数码管设置
      beepd(500,333);                                       //蜂鸣器响
      geshan();                                             //3
      beepd(300,555);                                       //延时
   }
   else if(digitalRead(key[3])==down)                       //按键3按下
   {
      delay(10);                                            //延时
      while(digitalRead(key[3])==down);                     //等待按键3松开
      shumaguanshezhi(3,4);                                 //数码管设置
      beepd(500,333);                                       //蜂鸣器响
      niganma();                                            //4
      beepd(300,555);                                       //延时
   }                              
}
